#include "porting.h"

#include "touch/gt911.h"
#include "touch/CST3240.h"
#include "touch/FT62XX.h"

#include "EspI2C.h"

static const char *TAG = "porting";

void touchpad_init()
{
#ifdef USE_ESP_I2C
    EspI2C::GetInstance()->Init(I2C_NUM_0, GPIO_I2C_SDA, GPIO_I2C_SCL);
#else
    ESP_ERROR_CHECK(bsp_i2c_init(I2C_NUM_0, 400000));
#endif

#if GT911
    ESP_ERROR_CHECK(gt911_init());
#elif FT62XX
    ESP_ERROR_CHECK(FT62XX_init());
#elif CST3240
    ESP_ERROR_CHECK(CST3240_init());
#else
    ESP_LOGI("No touchpad detected");
#endif
}

// static bool touchpad_event_callback()
// {
//     static int s_last_mouse_x = 0;
//     static int s_last_mouse_y = 0;
//     static bool s_mouse_pressed = false;
//     /*Save the pressed coordinates and the state*/
//     if (touchpad_is_pressed())
//     {
//         touchpad_get_xy(&s_last_mouse_x, &s_last_mouse_y);
//         s_mouse_pressed = true;
//     }
//     else
//     {
//         if (s_mouse_pressed)
//         {

//         }
//         s_mouse_pressed = false;
//     }
// }

bool touchpad_is_pressed(void)
{
    uint8_t temp = 0;
#if GT911
    gt911_read_bytes(GT_GSTID_REG, 1, &temp);
    // ESP_LOGI(TAG, "temp=0x%X",temp);
    if (temp & 0x80)
    {
        if (temp & 0x0F)
        {
            return true;
        }
        temp = 0;
        gt911_write_bytes(GT_GSTID_REG, 1, &temp);
    }
    return false;
#elif FT62XX
    FT62XX_read_bytes(FT_REG_NUM_FINGER, 1, &temp);
    // ESP_LOGI(TAG, "temp=0x%X",temp);
    if ((temp & 0XF) && ((temp & 0XF) < 6))
    {
        if (temp & 0XF)
        {
            return true;
        }
    }
    return false;
#elif CST3240
    CST3240_read_bytes(CST_GSTID_REG, 1, &temp);
    // ESP_LOGI(TAG, "temp=0x%X",temp);
    if ((temp & 0XF) && ((temp & 0XF) < 6))
    {
        if (temp & 0XF)
        {
            return true;
        }
    }
    return false;
#else
    return false;
#endif
}

void touchpad_get_xy(int *x, int *y)
{
#if GT911
    uint8_t data[4];
    gt911_read_bytes(GT_TP1_REG, 4, data);
    *x = ((data[1] & 0x0f) << 8) + data[0];
    *y = ((data[3] & 0x0f) << 8) + data[2];
    // ESP_LOGI(TAG, "x=%d, y=%d",*x, *y);
    data[0] = 0;
    gt911_write_bytes(GT_GSTID_REG, 1, data);
#elif FT62XX
    uint8_t buf[4];
    uint16_t ctpx[5];
    uint16_t xtpy[5];

    // 读取XY坐标值
    FT62XX_read_bytes(0x03, 1, &buf[0]);
    xtpy[0] = (int16_t)(buf[0] & 0x0F) << 8;
    FT62XX_read_bytes(0x04, 1, &buf[1]);
    xtpy[0] |= (int16_t)buf[1];

    FT62XX_read_bytes(0x05, 1, &buf[2]);
    ctpx[0] = (int16_t)(buf[2] & 0x0F) << 8;
    FT62XX_read_bytes(0x06, 1, &buf[3]);
    ctpx[0] |= (int16_t)buf[3];

    *y = ctpx[0];
    *x = xtpy[0];

    // ESP_LOGI(TAG, "x=0x%X, y=%d",*x, *y);
#elif CST3240
    static uint8_t data[4];

    CST3240_read_bytes(CST_TP1_REG, 4, data);
    if (1) // 竖屏
    {
        *x = ((uint16_t)data[0] << 4) + (((uint16_t)data[2] >> 4) & 0x0F);
        *y = (((uint16_t)data[1] << 4) + ((uint16_t)data[2] & 0x0f));
    }
    else // 横屏
    {
        *y = ((uint16_t)data[0] << 4) + (((uint16_t)data[2] >> 4) & 0x0F);
        *x = (((uint16_t)data[3] << 8) + data[2]);
    }
    // ESP_LOGI(TAG, "x=%d, y=%d",*x, *y);
    data[0] = 0;
    CST3240_write_bytes(CST_GSTID_REG, 1, data); // 清标志
#else
#endif
}
